import URL from './url.js'


export default class course {
    //根据用户id获取所学习的课程
    get_course(username) {
        let keymap = {
            method: 'GET',
            headers: { 'UserName': username },
        };
        return new Promise((resolve, reject) => {
            fetch(URL.STUDY_COURSE_URL, keymap).then((result) => {
                result.json().then(
                    (resultData) => {
                        if (resultData != undefined && resultData.length > 0) {
                            let course=[];
                            for(let i=resultData.length-1;i>=0;i--){
                                if(resultData[i].termname==="2017年夏"){
                                    course.push(resultData[i]);
                                }
                                if(resultData[i].termname==="2017年春"){
                                    resultData[i].courseList.forEach(function(element){
                                        if(element.title==="金水区中小学校长教育信息化领导力提升高级研修班"){
                                            element.title="教育信息化领导力提升高级研修班";
                                            resultData[i].courseList=[element];
                                        }                    
                                    })
                                    course.push(resultData[i]);
                                }
                            }
                            console.log('course20171206');
                            console.log(course);
                            resolve(course);
                        } else
                            resolve(undefined);
                    }
                ).catch(
                    (error) => { reject(error); })
            }).catch((err) => { reject(err) })

        });
    }
    //根据课程id获取章节列表
    get_course_info(courseid) {
        let keymap = {
            method: 'GET',
            headers: { 'courseid': courseid },
        };
        return new Promise((resolve, reject) => {
            fetch(URL.COURSE_INFO_URL, keymap).then((result) => {//URL.COURSE_INFO_URL
                result.json().then(
                    (resultData) => {
                        let outArray = [];
                        resultData.forEach((item) => {
                            if (item.topicType == '1') {
                                outArray.push(item);
                            }
                        });
                        resolve(outArray);
                    }
                ).catch(
                    (error) => { reject(error); })
            }).catch((err) => { reject(err) })
        });

    }
    //华师的id是1，根据地域选择学校院系
    get_department() {
        let mapkey = {
            method: 'GET',
            headers: { 'reignid': 1 },
        }
        return new Promise((resolve, reject) => {
            fetch(URL.MORE_COURSE_DEPARTMENT_URL, mapkey).then((result) => {
                result.json().then(
                    (resultData) => {
                        resolve(resultData);
                    }
                ).catch(
                    (error) => { reject(error); })
            }).catch((err) => { reject(err) })

        });
    }
    //根据学院id获取学院中的课程
    get_list(departmentid) {
        let mapkey = {
            method: 'GET',
            headers: { 'dptcode': departmentid },
        }
        return new Promise((resolve, reject) => {
            fetch(URL.MORE_COURSE_LIST_URL, mapkey).then((result) => {
                result.json().then(
                    (resultData) => {
                        resolve(resultData);
                    }
                ).catch(
                    (error) => { reject(error); })
            }).catch((err) => { reject(err) })
        });
    }

    get_term() {
        let mapkey = {
            method: 'GET',
        }
        return new Promise((resolve, reject) => {
            fetch(URL.TERM_URL, mapkey).then((result) => {
                result.json().then(
                    (resultData) => {
                        if (resultData != undefined && resultData.length > 0) {
                            let terms=[];
                            for(let i=resultData.length-1;i>=0;i--){
                                terms.push(resultData[i]);
                            }
                            resolve(terms);
                        }
                        else {
                            resolve(undefined);
                        }
                    }
                ).catch(
                    (error) => { reject(error); })
            }).catch((err) => { reject(err) })

        });
    }
    //根据课程id和学期获取课程信息
    get_more_course_info(courseid, termvalue) {
        let mapkey = {
            method: 'GET',
            headers: {
                'courseid': courseid,
                'termcode': termvalue
            }
        }
        return new Promise((resolve, reject) => {
            fetch(URL.MORE_COURSE_TERM_URL, mapkey).then((result) => {
                result.json().then(
                    (resultData) => {
                        if (resultData != undefined && resultData.length > 0) {
                            resolve(resultData);
                        }
                        else {
                            resolve(undefined);
                        }
                    }
                ).catch(
                    (error) => { reject(error); })
            }).catch((err) => { reject(err) })

        });
    }
    get_resource(subchapter) {
      /*   let mapkey = {
            method: 'GET',
            headers: {
                'unitid': subchapter,
            }
        } */
        return new Promise((resolve, reject) => {
    /*         fetch(URL.GET_RESOURCE_URL, mapkey).then((result) => {
                result.json().then(
                    (resultData) => {
                        if (resultData != undefined && resultData.length > 0) {
                            console.log(resultData);
                            resolve(resultData);
                        }
                        else {
                            resolve(undefined);
                        }
                    }
                ).catch(
                    (error) => { reject(error); })
            }).catch((err) => { reject(err) }) */
            resultData=[
                {
                    "coursewareId":"8093893",
                    "activityFolder":[
                        {
                            "fileFolder":[
                                {
                                    "fileId":"2146596",
                                    "fileType":2,
                                    "fileName":"java简介.wmv",
                                    "url":"http://119.97.166.135:80/data/userdata/vod/resource/201705/RVVYvHC3.mp4"
                                },
                                {
                                    "fileId":"2146598",
                                    "fileType":1,
                                    "fileName":"01 面向对象概述.ppt",
                                    "url":"http://202.114.33.120:8066/MoocFileService/DownloadFile/object-2015_09_17_20_11_22_4670_4330.ppt"
                                }
                            ],
                            "activityId":"8093894",
                            "activityTitle":"新活动"
                        }
                    ],
                    "coursewareTitle":"新课件"
                }
            ];
            resolve(resultData);
        });
    }
}
